home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / src / net / readline-2.0mg.tar.gz / readline-2.0mg.tar / readline-2.0mg / bind.c < prev    next >
C/C++ Source or Header  |  1994-11-20  |  36KB  |  1,492 lines

  1. /* bind.c -- key binding and startup file support for the readline library. */
  2.  
  3. /* Copyright (C) 1987, 1989, 1992 Free Software Foundation, Inc.
  4.  
  5.    This file is part of the GNU Readline Library, a library for
  6.    reading lines of text with interactive input and history editing.
  7.  
  8.    The GNU Readline Library is free software; you can redistribute it
  9.    and/or modify it under the terms of the GNU General Public License
  10.    as published by the Free Software Foundation; either version 1, or
  11.    (at your option) any later version.
  12.  
  13.    The GNU Readline Library is distributed in the hope that it will be
  14.    useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  15.    of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.    GNU General Public License for more details.
  17.  
  18.    The GNU General Public License is often shipped with GNU software, and
  19.    is generally kept in a file called COPYING or LICENSE.  If you do not
  20.    have a copy of the license, write to the Free Software Foundation,
  21.    675 Mass Ave, Cambridge, MA 02139, USA. */
  22. #define READLINE_LIBRARY
  23.  
  24. #if defined (HAVE_CONFIG_H)
  25. #  include "config.h"
  26. #endif
  27.  
  28. #include <stdio.h>
  29. #include <sys/types.h>
  30. #include <fcntl.h>
  31. #if !defined (NO_SYS_FILE)
  32. #  include <sys/file.h>
  33. #endif /* !NO_SYS_FILE */
  34. #include <signal.h>
  35.  
  36. #if defined (HAVE_UNISTD_H)
  37. #  include <unistd.h>
  38. #endif /* HAVE_UNISTD_H */
  39.  
  40. #if defined (HAVE_STDLIB_H)
  41. #  include <stdlib.h>
  42. #else
  43. #  include "ansi_stdlib.h"
  44. #endif /* HAVE_STDLIB_H */
  45.  
  46. #include <errno.h>
  47. /* Not all systems declare ERRNO in errno.h... and some systems #define it! */
  48. #if !defined (errno)
  49. extern int errno;
  50. #endif /* !errno */
  51.  
  52. #include "posixstat.h"
  53.  
  54. /* System-specific feature definitions and include files. */
  55. #include "rldefs.h"
  56.  
  57. /* Some standard library routines. */
  58. #include "readline.h"
  59. #include "history.h"
  60.  
  61. #if !defined (strchr) && !defined (__STDC__)
  62. extern char *strchr (), *strrchr ();
  63. #endif /* !strchr && !__STDC__ */
  64.  
  65. extern int _rl_horizontal_scroll_mode;
  66. extern int _rl_mark_modified_lines;
  67. extern int _rl_bell_preference;
  68. extern int _rl_meta_flag;
  69. extern int _rl_convert_meta_chars_to_ascii;
  70. extern int _rl_output_meta_chars;
  71. extern int _rl_complete_show_all;
  72. #if defined (PAREN_MATCHING)
  73. extern int rl_blink_matching_paren;
  74. #endif /* PAREN_MATCHING */
  75. #if defined (VISIBLE_STATS)
  76. extern int rl_visible_stats;
  77. #endif /* VISIBLE_STATS */
  78. extern int rl_complete_with_tilde_expansion;
  79. extern int rl_completion_query_items;
  80. #if defined (VI_MODE)
  81. extern char *rl_vi_comment_begin;
  82. #endif
  83.  
  84. extern int rl_explicit_arg;
  85. extern int rl_editing_mode;
  86. extern unsigned short _rl_parsing_conditionalized_out;
  87. extern Keymap _rl_keymap;
  88.  
  89. extern char *possible_control_prefixes[], *possible_meta_prefixes[];
  90.  
  91. extern char **rl_funmap_names ();
  92.  
  93. /* Forward declarations */
  94. void rl_set_keymap_from_edit_mode ();
  95.  
  96. static int glean_key_from_name ();
  97.  
  98. #if defined (HAVE_STRCASECMP)
  99. #define stricmp strcasecmp
  100. #define strnicmp strncasecmp
  101. #else
  102. static int stricmp (), strnicmp ();
  103. #endif
  104.  
  105. #if defined (STATIC_MALLOC)
  106. static char *xmalloc (), *xrealloc ();
  107. #else
  108. extern char *xmalloc (), *xrealloc ();
  109. #endif /* STATIC_MALLOC */
  110.  
  111. /* **************************************************************** */
  112. /*                                    */
  113. /*            Binding keys                    */
  114. /*                                    */
  115. /* **************************************************************** */
  116.  
  117. /* rl_add_defun (char *name, Function *function, int key)
  118.    Add NAME to the list of named functions.  Make FUNCTION be the function
  119.    that gets called.  If KEY is not -1, then bind it. */
  120. rl_add_defun (name, function, key)
  121.      char *name;
  122.      Function *function;
  123.      int key;
  124. {
  125.   if (key != -1)
  126.     rl_bind_key (key, function);
  127.   rl_add_funmap_entry (name, function);
  128.   return 0;
  129. }
  130.  
  131. /* Bind KEY to FUNCTION.  Returns non-zero if KEY is out of range. */
  132. int
  133. rl_bind_key (key, function)
  134.      int key;
  135.      Function *function;
  136. {
  137.   if (key < 0)
  138.     return (key);
  139.  
  140.   if (META_CHAR (key) && _rl_convert_meta_chars_to_ascii)
  141.     {
  142.       if (_rl_keymap[ESC].type == ISKMAP)
  143.     {
  144.       Keymap escmap;
  145.  
  146.       escmap = FUNCTION_TO_KEYMAP (_rl_keymap, ESC);
  147.       key = UNMETA (key);
  148.       escmap[key].type = ISFUNC;
  149.       escmap[key].function = function;
  150.       return (0);
  151.     }
  152.       return (key);
  153.     }
  154.  
  155.   _rl_keymap[key].type = ISFUNC;
  156.   _rl_keymap[key].function = function;
  157.   return (0);
  158. }
  159.  
  160. /* Bind KEY to FUNCTION in MAP.  Returns non-zero in case of invalid
  161.    KEY. */
  162. int
  163. rl_bind_key_in_map (key, function, map)
  164.      int key;
  165.      Function *function;
  166.      Keymap map;
  167. {
  168.   int result;
  169.   Keymap oldmap = _rl_keymap;
  170.  
  171.   _rl_keymap = map;
  172.   result = rl_bind_key (key, function);
  173.   _rl_keymap = oldmap;
  174.   return (result);
  175. }
  176.  
  177. /* Make KEY do nothing in the currently selected keymap.
  178.    Returns non-zero in case of error. */
  179. int
  180. rl_unbind_key (key)
  181.      int key;
  182. {
  183.   return (rl_bind_key (key, (Function *)NULL));
  184. }
  185.  
  186. /* Make KEY do nothing in MAP.
  187.    Returns non-zero in case of error. */
  188. int
  189. rl_unbind_key_in_map (key, map)
  190.      int key;
  191.      Keymap map;
  192. {
  193.   return (rl_bind_key_in_map (key, (Function *)NULL, map));
  194. }
  195.  
  196. /* Bind the key sequence represented by the string KEYSEQ to
  197.    FUNCTION.  This makes new keymaps as necessary.  The initial
  198.    place to do bindings is in MAP. */
  199. rl_set_key (keyseq, function, map)
  200.      char *keyseq;
  201.      Function *function;
  202.      Keymap map;
  203. {
  204.   return (rl_generic_bind (ISFUNC, keyseq, function, map));
  205. }
  206.  
  207. /* Bind the key sequence represented by the string KEYSEQ to
  208.    the string of characters MACRO.  This makes new keymaps as
  209.    necessary.  The initial place to do bindings is in MAP. */
  210. rl_macro_bind (keyseq, macro, map)
  211.      char *keyseq, *macro;
  212.      Keymap map;
  213. {
  214.   char *macro_keys;
  215.   int macro_keys_len;
  216.  
  217.   macro_keys = (char *)xmalloc ((2 * strlen (macro)) + 1);
  218.  
  219.   if (rl_translate_keyseq (macro, macro_keys, ¯o_keys_len))
  220.     {
  221.       free (macro_keys);
  222.       return -1;
  223.     }
  224.   rl_generic_bind (ISMACR, keyseq, macro_keys, map);
  225.   return 0;
  226. }
  227.  
  228. /* Bind the key sequence represented by the string KEYSEQ to
  229.    the arbitrary pointer DATA.  TYPE says what kind of data is
  230.    pointed to by DATA, right now this can be a function (ISFUNC),
  231.    a macro (ISMACR), or a keymap (ISKMAP).  This makes new keymaps
  232.    as necessary.  The initial place to do bindings is in MAP. */
  233. rl_generic_bind (type, keyseq, data, map)
  234.      int type;
  235.      char *keyseq, *data;
  236.      Keymap map;
  237. {
  238.   char *keys;
  239.   int keys_len;
  240.   register int i;
  241.  
  242.   /* If no keys to bind to, exit right away. */
  243.   if (!keyseq || !*keyseq)
  244.     {
  245.       if (type == ISMACR)
  246.     free (data);
  247.       return -1;
  248.     }
  249.  
  250.   keys = xmalloc (1 + (2 * strlen (keyseq)));
  251.  
  252.   /* Translate the ASCII representation of KEYSEQ into an array of
  253.      characters.  Stuff the characters into KEYS, and the length of
  254.      KEYS into KEYS_LEN. */
  255.   if (rl_translate_keyseq (keyseq, keys, &keys_len))
  256.     {
  257.       free (keys);
  258.       return -1;
  259.     }
  260.  
  261.   /* Bind keys, making new keymaps as necessary. */
  262.   for (i = 0; i < keys_len; i++)
  263.     {
  264.       int ic = (int) ((unsigned char)keys[i]);
  265.  
  266.       if (_rl_convert_meta_chars_to_ascii && META_CHAR (ic))
  267.     {
  268.       ic = UNMETA (ic);
  269.       if (map[ESC].type == ISKMAP)
  270.         map = FUNCTION_TO_KEYMAP (map, ESC);
  271.     }
  272.  
  273.       if ((i + 1) < keys_len)
  274.     {
  275.       if (map[ic].type != ISKMAP)
  276.         {
  277.           if (map[ic].type == ISMACR)
  278.         free ((char *)map[ic].function);
  279.  
  280.           map[ic].type = ISKMAP;
  281.           map[ic].function = KEYMAP_TO_FUNCTION (rl_make_bare_keymap());
  282.         }
  283.       map = FUNCTION_TO_KEYMAP (map, ic);
  284.     }
  285.       else
  286.     {
  287.       if (map[ic].type == ISMACR)
  288.         free ((char *)map[ic].function);
  289.  
  290.       map[ic].function = KEYMAP_TO_FUNCTION (data);
  291.       map[ic].type = type;
  292.     }
  293.     }
  294.   free (keys);
  295.   return 0;
  296. }
  297.  
  298. /* Translate the ASCII representation of SEQ, stuffing the values into ARRAY,
  299.    an array of characters.  LEN gets the final length of ARRAY.  Return
  300.    non-zero if there was an error parsing SEQ. */
  301. rl_translate_keyseq (seq, array, len)
  302.      char *seq, *array;
  303.      int *len;
  304. {
  305.   register int i, c, l = 0;
  306.  
  307.   for (i = 0; c = seq[i]; i++)
  308.     {
  309.       if (c == '\\')
  310.     {
  311.       c = seq[++i];
  312.  
  313.       if (!c)
  314.         break;
  315.  
  316.       if (((c == 'C' || c == 'M') &&  seq[i + 1] == '-') ||
  317.           (c == 'e'))
  318.         {
  319.           /* Handle special case of backwards define. */
  320.           if (strncmp (&seq[i], "C-\\M-", 5) == 0)
  321.         {
  322.           array[l++] = ESC;
  323.           i += 5;
  324.           array[l++] = CTRL (to_upper (seq[i]));
  325.           if (!seq[i])
  326.             i--;
  327.           continue;
  328.         }
  329.  
  330.           switch (c)
  331.         {
  332.         case 'M':
  333.           i++;
  334.           array[l++] = ESC;
  335.           break;
  336.  
  337.         case 'C':
  338.           i += 2;
  339.           /* Special hack for C-?... */
  340.           if (seq[i] == '?')
  341.             array[l++] = RUBOUT;
  342.           else
  343.             array[l++] = CTRL (to_upper (seq[i]));
  344.           break;
  345.  
  346.         case 'e':
  347.           array[l++] = ESC;
  348.         }
  349.  
  350.           continue;
  351.         }
  352.     }
  353.       array[l++] = c;
  354.     }
  355.  
  356.   *len = l;
  357.   array[l] = '\0';
  358.   return (0);
  359. }
  360.  
  361. /* Return a pointer to the function that STRING represents.
  362.    If STRING doesn't have a matching function, then a NULL pointer
  363.    is returned. */
  364. Function *
  365. rl_named_function (string)
  366.      char *string;
  367. {
  368.   register int i;
  369.  
  370.   rl_initialize_funmap ();
  371.  
  372.   for (i = 0; funmap[i]; i++)
  373.     if (stricmp (funmap[i]->name, string) == 0)
  374.       return (funmap[i]->function);
  375.   return ((Function *)NULL);
  376. }
  377.  
  378. /* Return the function (or macro) definition which would be invoked via
  379.    KEYSEQ if executed in MAP.  If MAP is NULL, then the current keymap is
  380.    used.  TYPE, if non-NULL, is a pointer to an int which will receive the
  381.    type of the object pointed to.  One of ISFUNC (function), ISKMAP (keymap),
  382.    or ISMACR (macro). */
  383. Function *
  384. rl_function_of_keyseq (keyseq, map, type)
  385.      char *keyseq;
  386.      Keymap map;
  387.      int *type;
  388. {
  389.   register int i;
  390.  
  391.   if (!map)
  392.     map = _rl_keymap;
  393.  
  394.   for (i = 0; keyseq && keyseq[i]; i++)
  395.     {
  396.       int ic = keyseq[i];
  397.  
  398.       if (META_CHAR (ic) && _rl_convert_meta_chars_to_ascii)
  399.     {
  400.       if (map[ESC].type != ISKMAP)
  401.         {
  402.           if (type)
  403.         *type = map[ESC].type;
  404.  
  405.           return (map[ESC].function);
  406.         }
  407.       else
  408.         {
  409.           map = FUNCTION_TO_KEYMAP (map, ESC);
  410.           ic = UNMETA (ic);
  411.         }
  412.     }
  413.  
  414.       if (map[ic].type == ISKMAP)
  415.     {
  416.       /* If this is the last key in the key sequence, return the
  417.          map. */
  418.       if (!keyseq[i + 1])
  419.         {
  420.           if (type)
  421.         *type = ISKMAP;
  422.  
  423.           return (map[ic].function);
  424.         }
  425.       else
  426.         map = FUNCTION_TO_KEYMAP (map, ic);
  427.     }
  428.       else
  429.     {
  430.       if (type)
  431.         *type = map[ic].type;
  432.  
  433.       return (map[ic].function);
  434.     }
  435.     }
  436.   return ((Function *) NULL);
  437. }
  438.  
  439. /* The last key bindings file read. */
  440. static char *last_readline_init_file = (char *)NULL;
  441.  
  442. /* Re-read the current keybindings file. */
  443. rl_re_read_init_file (count, ignore)
  444.      int count, ignore;
  445. {
  446.   int r;
  447.   r = rl_read_init_file ((char *)NULL);
  448.   rl_set_keymap_from_edit_mode ();
  449.   return r;
  450. }
  451.  
  452. /* Do key bindings from a file.  If FILENAME is NULL it defaults
  453.    to the first non-null filename from this list:
  454.      1. the filename used for the previous call
  455.      2. the value of the shell variable `INPUTRC'
  456.      3. ~/.inputrc
  457.    If the file existed and could be opened and read, 0 is returned,
  458.    otherwise errno is returned. */
  459. int
  460. rl_read_init_file (filename)
  461.      char *filename;
  462. {
  463.   register int i;
  464.   char *buffer, *openname, *line, *end;
  465.   struct stat finfo;
  466.   int file;
  467.  
  468.   /* Default the filename. */
  469.   if (!filename)
  470.     {
  471.       filename = last_readline_init_file;
  472.       if (!filename)
  473.         filename = getenv ("INPUTRC");
  474.       if (!filename)
  475.     filename = DEFAULT_INPUTRC;
  476.     }
  477.  
  478.   if (!*filename)
  479.     filename = DEFAULT_INPUTRC;
  480.  
  481.   openname = tilde_expand (filename);
  482.  
  483.   if ((stat (openname, &finfo) < 0) ||
  484.       (file = open (openname, O_RDONLY, 0666)) < 0)
  485.     {
  486.       free (openname);
  487.       return (errno);
  488.     }
  489.   else
  490.     free (openname);
  491.  
  492.   if (filename != last_readline_init_file)
  493.     {
  494.       if (last_readline_init_file)
  495.     free (last_readline_init_file);
  496.  
  497.       last_readline_init_file = savestring (filename);
  498.     }
  499.  
  500.   /* Read the file into BUFFER. */
  501.   buffer = (char *)xmalloc ((int)finfo.st_size + 1);
  502.   i = read (file, buffer, finfo.st_size);
  503.   close (file);
  504.  
  505.   if (i != finfo.st_size)
  506.     return (errno);
  507.  
  508.   /* Loop over the lines in the file.  Lines that start with `#' are
  509.      comments; all other lines are commands for readline initialization. */
  510.   line = buffer;
  511.   end = buffer + finfo.st_size;
  512.   while (line < end)
  513.     {
  514.       /* Find the end of this line. */
  515.       for (i = 0; line + i != end && line[i] != '\n'; i++);
  516.  
  517.       /* Mark end of line. */
  518.       line[i] = '\0';
  519.  
  520.       /* Skip leading whitespace. */
  521.       while (*line && whitespace (*line))
  522.         {
  523.       line++;
  524.       i--;
  525.         }
  526.  
  527.       /* If the line is not a comment, then parse it. */
  528.       if (*line && *line != '#')
  529.     rl_parse_and_bind (line);
  530.  
  531.       /* Move to the next line. */
  532.       line += i + 1;
  533.     }
  534.   free (buffer);
  535.   return (0);
  536. }
  537.  
  538. /* **************************************************************** */
  539. /*                                    */
  540. /*            Parser Directives                   */
  541. /*                                    */
  542. /* **************************************************************** */
  543.  
  544. /* Conditionals. */
  545.  
  546. /* Calling programs set this to have their argv[0]. */
  547. char *rl_readline_name = "other";
  548.  
  549. /* Stack of previous values of parsing_conditionalized_out. */
  550. static unsigned char *if_stack = (unsigned char *)NULL;
  551. static int if_stack_depth = 0;
  552. static int if_stack_size = 0;
  553.  
  554. /* Push _rl_parsing_conditionalized_out, and set parser state based
  555.    on ARGS. */
  556. static int
  557. parser_if (args)
  558.      char *args;
  559. {
  560.   register int i;
  561.  
  562.   /* Push parser state. */
  563.   if (if_stack_depth + 1 >= if_stack_size)
  564.     {
  565.       if (!if_stack)
  566.     if_stack = (unsigned char *)xmalloc (if_stack_size = 20);
  567.       else
  568.     if_stack = (unsigned char *)xrealloc (if_stack, if_stack_size += 20);
  569.     }
  570.   if_stack[if_stack_depth++] = _rl_parsing_conditionalized_out;
  571.  
  572.   /* If parsing is turned off, then nothing can turn it back on except
  573.      for finding the matching endif.  In that case, return right now. */
  574.   if (_rl_parsing_conditionalized_out)
  575.     return 0;
  576.  
  577.   /* Isolate first argument. */
  578.   for (i = 0; args[i] && !whitespace (args[i]); i++);
  579.  
  580.   if (args[i])
  581.     args[i++] = '\0';
  582.  
  583.   /* Handle "if term=foo" and "if mode=emacs" constructs.  If this
  584.      isn't term=foo, or mode=emacs, then check to see if the first
  585.      word in ARGS is the same as the value stored in rl_readline_name. */
  586.   if (rl_terminal_name && strnicmp (args, "term=", 5) == 0)
  587.     {
  588.       char *tem, *tname;
  589.  
  590.       /* Terminals like "aaa-60" are equivalent to "aaa". */
  591.       tname = savestring (rl_terminal_name);
  592.       tem = strchr (tname, '-');
  593.       if (tem)
  594.     *tem = '\0';
  595.  
  596.       /* Test the `long' and `short' forms of the terminal name so that
  597.      if someone has a `sun-cmd' and does not want to have bindings
  598.      that will be executed if the terminal is a `sun', they can put
  599.      `$if term=sun-cmd' into their .inputrc. */
  600.       if ((stricmp (args + 5, tname) == 0) ||
  601.       (stricmp (args + 5, rl_terminal_name) == 0))
  602.     _rl_parsing_conditionalized_out = 0;
  603.       else
  604.     _rl_parsing_conditionalized_out = 1;
  605.  
  606.       free (tname);
  607.     }
  608. #if defined (VI_MODE)
  609.   else if (strnicmp (args, "mode=", 5) == 0)
  610.     {
  611.       int mode;
  612.  
  613.       if (stricmp (args + 5, "emacs") == 0)
  614.     mode = emacs_mode;
  615.       else if (stricmp (args + 5, "vi") == 0)
  616.     mode = vi_mode;
  617.       else
  618.     mode = no_mode;
  619.  
  620.       if (mode == rl_editing_mode)
  621.     _rl_parsing_conditionalized_out = 0;
  622.       else
  623.     _rl_parsing_conditionalized_out = 1;
  624.     }
  625. #endif /* VI_MODE */
  626.   /* Check to see if the first word in ARGS is the same as the
  627.      value stored in rl_readline_name. */
  628.   else if (stricmp (args, rl_readline_name) == 0)
  629.     _rl_parsing_conditionalized_out = 0;
  630.   else
  631.     _rl_parsing_conditionalized_out = 1;
  632.   return 0;
  633. }
  634.  
  635. /* Invert the current parser state if there is anything on the stack. */
  636. static int
  637. parser_else (args)
  638.      char *args;
  639. {
  640.   register int i;
  641.  
  642.   if (!if_stack_depth)
  643.     {
  644.       /* Error message? */
  645.       return 0;
  646.     }
  647.  
  648.   /* Check the previous (n - 1) levels of the stack to make sure that
  649.      we haven't previously turned off parsing. */
  650.   for (i = 0; i < if_stack_depth - 1; i++)
  651.     if (if_stack[i] == 1)
  652.       return 0;
  653.  
  654.   /* Invert the state of parsing if at top level. */
  655.   _rl_parsing_conditionalized_out = !_rl_parsing_conditionalized_out;
  656.   return 0;
  657. }
  658.  
  659. /* Terminate a conditional, popping the value of
  660.    _rl_parsing_conditionalized_out from the stack. */
  661. static int
  662. parser_endif (args)
  663.      char *args;
  664. {
  665.   if (if_stack_depth)
  666.     _rl_parsing_conditionalized_out = if_stack[--if_stack_depth];
  667.   else
  668.     {
  669.       /* *** What, no error message? *** */
  670.     }
  671.   return 0;
  672. }
  673.  
  674. /* Associate textual names with actual functions. */
  675. static struct {
  676.   char *name;
  677.   Function *function;
  678. } parser_directives [] = {
  679.   { "if", parser_if },
  680.   { "endif", parser_endif },
  681.   { "else", parser_else },
  682.   { (char *)0x0, (Function *)0x0 }
  683. };
  684.  
  685. /* Handle a parser directive.  STATEMENT is the line of the directive
  686.    without any leading `$'. */
  687. static int
  688. handle_parser_directive (statement)
  689.      char *statement;
  690. {
  691.   register int i;
  692.   char *directive, *args;
  693.  
  694.   /* Isolate the actual directive. */
  695.  
  696.   /* Skip whitespace. */
  697.   for (i = 0; whitespace (statement[i]); i++);
  698.  
  699.   directive = &statement[i];
  700.  
  701.   for (; statement[i] && !whitespace (statement[i]); i++);
  702.  
  703.   if (statement[i])
  704.     statement[i++] = '\0';
  705.  
  706.   for (; statement[i] && whitespace (statement[i]); i++);
  707.  
  708.   args = &statement[i];
  709.  
  710.   /* Lookup the command, and act on it. */
  711.   for (i = 0; parser_directives[i].name; i++)
  712.     if (stricmp (directive, parser_directives[i].name) == 0)
  713.       {
  714.     (*parser_directives[i].function) (args);
  715.     return (0);
  716.       }
  717.  
  718.   /* *** Should an error message be output? */
  719.   return (1);
  720. }
  721.  
  722. static int substring_member_of_array ();
  723.  
  724. /* Read the binding command from STRING and perform it.
  725.    A key binding command looks like: Keyname: function-name\0,
  726.    a variable binding command looks like: set variable value.
  727.    A new-style keybinding looks like "\C-x\C-x": exchange-point-and-mark. */
  728. rl_parse_and_bind (string)
  729.      char *string;
  730. {
  731.   char *funname, *kname;
  732.   register int c, i;
  733.   int key, equivalency;
  734.  
  735.   while (string && whitespace (*string))
  736.     string++;
  737.  
  738.   if (!string || !*string || *string == '#')
  739.     return 0;
  740.  
  741.   /* If this is a parser directive, act on it. */
  742.   if (*string == '$')
  743.     {
  744.       handle_parser_directive (&string[1]);
  745.       return 0;
  746.     }
  747.  
  748.   /* If we aren't supposed to be parsing right now, then we're done. */
  749.   if (_rl_parsing_conditionalized_out)
  750.     return 0;
  751.  
  752.   i = 0;
  753.   /* If this keyname is a complex key expression surrounded by quotes,
  754.      advance to after the matching close quote.  This code allows the
  755.      backslash to quote characters in the key expression. */
  756.   if (*string == '"')
  757.     {
  758.       int passc = 0;
  759.  
  760.       for (i = 1; c = string[i]; i++)
  761.     {
  762.       if (passc)
  763.         {
  764.           passc = 0;
  765.           continue;
  766.         }
  767.  
  768.       if (c == '\\')
  769.         {
  770.           passc++;
  771.           continue;
  772.         }
  773.  
  774.       if (c == '"')
  775.         break;
  776.     }
  777.     }
  778.  
  779.   /* Advance to the colon (:) or whitespace which separates the two objects. */
  780.   for (; (c = string[i]) && c != ':' && c != ' ' && c != '\t'; i++ );
  781.  
  782.   equivalency = (c == ':' && string[i + 1] == '=');
  783.  
  784.   /* Mark the end of the command (or keyname). */
  785.   if (string[i])
  786.     string[i++] = '\0';
  787.  
  788.   /* If doing assignment, skip the '=' sign as well. */
  789.   if (equivalency)
  790.     string[i++] = '\0';
  791.  
  792.   /* If this is a command to set a variable, then do that. */
  793.   if (stricmp (string, "set") == 0)
  794.     {
  795.       char *var = string + i;
  796.       char *value;
  797.  
  798.       /* Make VAR point to start of variable name. */
  799.       while (*var && whitespace (*var)) var++;
  800.  
  801.       /* Make value point to start of value string. */
  802.       value = var;
  803.       while (*value && !whitespace (*value)) value++;
  804.       if (*value)
  805.     *value++ = '\0';
  806.       while (*value && whitespace (*value)) value++;
  807.  
  808.       rl_variable_bind (var, value);
  809.       return 0;
  810.     }
  811.  
  812.   /* Skip any whitespace between keyname and funname. */
  813.   for (; string[i] && whitespace (string[i]); i++);
  814.   funname = &string[i];
  815.  
  816.   /* Now isolate funname.
  817.      For straight function names just look for whitespace, since
  818.      that will signify the end of the string.  But this could be a
  819.      macro definition.  In that case, the string is quoted, so skip
  820.      to the matching delimiter.  We allow the backslash to quote the
  821.      delimiter characters in the macro body. */
  822.   /* This code exists to allow whitespace in macro expansions, which
  823.      would otherwise be gobbled up by the next `for' loop.*/
  824.   /* XXX - it may be desirable to allow backslash quoting only if " is
  825.      the quoted string delimiter, like the shell. */
  826.   if (*funname == '\'' || *funname == '"')
  827.     {
  828.       int delimiter = string[i++];
  829.       int passc = 0;
  830.  
  831.       for (; c = string[i]; i++)
  832.     {
  833.       if (passc)
  834.         {
  835.           passc = 0;
  836.           continue;
  837.         }
  838.  
  839.       if (c == '\\')
  840.         {
  841.           passc = 1;
  842.           continue;
  843.         }
  844.  
  845.       if (c == delimiter)
  846.         break;
  847.     }
  848.       if (c)
  849.     i++;
  850.     }
  851.  
  852.   /* Advance to the end of the string.  */
  853.   for (; string[i] && !whitespace (string[i]); i++);
  854.  
  855.   /* No extra whitespace at the end of the string. */
  856.   string[i] = '\0';
  857.  
  858.   /* Handle equivalency bindings here.  Make the left-hand side be exactly
  859.      whatever the right-hand evaluates to, including keymaps. */
  860.   if (equivalency)
  861.     {
  862.       return 0;
  863.     }
  864.  
  865.   /* If this is a new-style key-binding, then do the binding with
  866.      rl_set_key ().  Otherwise, let the older code deal with it. */
  867.   if (*string == '"')
  868.     {
  869.       char *seq = xmalloc (1 + strlen (string));
  870.       register int j, k = 0;
  871.       int passc = 0;
  872.  
  873.       for (j = 1; string[j]; j++)
  874.     {
  875.       /* Allow backslash to quote characters, but leave them in place.
  876.          This allows a string to end with a backslash quoting another
  877.          backslash, or with a backslash quoting a double quote.  The
  878.          backslashes are left in place for rl_translate_keyseq (). */
  879.       if (passc || (string[j] == '\\'))
  880.         {
  881.           seq[k++] = string[j];
  882.           passc = !passc;
  883.           continue;
  884.         }
  885.  
  886.       if (string[j] == '"')
  887.         break;
  888.  
  889.       seq[k++] = string[j];
  890.     }
  891.       seq[k] = '\0';
  892.  
  893.       /* Binding macro? */
  894.       if (*funname == '\'' || *funname == '"')
  895.     {
  896.       j = strlen (funname);
  897.  
  898.       /* Remove the delimiting quotes from each end of FUNNAME. */
  899.       if (j && funname[j - 1] == *funname)
  900.         funname[j - 1] = '\0';
  901.  
  902.       rl_macro_bind (seq, &funname[1], _rl_keymap);
  903.     }
  904.       else
  905.     rl_set_key (seq, rl_named_function (funname), _rl_keymap);
  906.  
  907.       free (seq);
  908.       return 0;
  909.     }
  910.  
  911.   /* Get the actual character we want to deal with. */
  912.   kname = strrchr (string, '-');
  913.   if (!kname)
  914.     kname = string;
  915.   else
  916.     kname++;
  917.  
  918.   key = glean_key_from_name (kname);
  919.  
  920.   /* Add in control and meta bits. */
  921.   if (substring_member_of_array (string, possible_control_prefixes))
  922.     key = CTRL (to_upper (key));
  923.  
  924.   if (substring_member_of_array (string, possible_meta_prefixes))
  925.     key = META (key);
  926.  
  927.   /* Temporary.  Handle old-style keyname with macro-binding. */
  928.   if (*funname == '\'' || *funname == '"')
  929.     {
  930.       char seq[2];
  931.       int fl = strlen (funname);
  932.  
  933.       seq[0] = key; seq[1] = '\0';
  934.       if (fl && funname[fl - 1] == *funname)
  935.     funname[fl - 1] = '\0';
  936.  
  937.       rl_macro_bind (seq, &funname[1], _rl_keymap);
  938.     }
  939. #if defined (PREFIX_META_HACK)
  940.   /* Ugly, but working hack to keep prefix-meta around. */
  941.   else if (stricmp (funname, "prefix-meta") == 0)
  942.     {
  943.       char seq[2];
  944.  
  945.       seq[0] = key;
  946.       seq[1] = '\0';
  947.       rl_generic_bind (ISKMAP, seq, (char *)emacs_meta_keymap, _rl_keymap);
  948.     }
  949. #endif /* PREFIX_META_HACK */
  950.   else
  951.     rl_bind_key (key, rl_named_function (funname));
  952.   return 0;
  953. }
  954.  
  955. /* Simple structure for boolean readline variables (i.e., those that can
  956.    have one of two values; either "On" or 1 for truth, or "Off" or 0 for
  957.    false. */
  958.  
  959. static struct {
  960.   char *name;
  961.   int *value;
  962. } boolean_varlist [] = {
  963.   { "horizontal-scroll-mode",    &_rl_horizontal_scroll_mode },
  964.   { "mark-modified-lines",    &_rl_mark_modified_lines },
  965.   { "meta-flag",        &_rl_meta_flag },
  966. #if defined (PAREN_MATCHING)
  967.   { "blink-matching-paren",    &rl_blink_matching_paren },
  968. #endif
  969.   { "convert-meta",        &_rl_convert_meta_chars_to_ascii },
  970.   { "show-all-if-ambiguous",    &_rl_complete_show_all },
  971.   { "output-meta",        &_rl_output_meta_chars },
  972. #if defined (VISIBLE_STATS)
  973.   { "visible-stats",        &rl_visible_stats },
  974. #endif /* VISIBLE_STATS */
  975.   { "expand-tilde",        &rl_complete_with_tilde_expansion },
  976.   { (char *)NULL, (int *)NULL }
  977. };
  978.  
  979. rl_variable_bind (name, value)
  980.      char *name, *value;
  981. {
  982.   register int i;
  983.  
  984.   /* Check for simple variables first. */
  985.   for (i = 0; boolean_varlist[i].name; i++)
  986.     {
  987.       if (stricmp (name, boolean_varlist[i].name) == 0)
  988.     {
  989.       /* A variable is TRUE if the "value" is "on", "1" or "". */
  990.       if ((!*value) ||
  991.           (stricmp (value, "On") == 0) ||
  992.           (value[0] == '1' && value[1] == '\0'))
  993.         *boolean_varlist[i].value = 1;
  994.       else
  995.         *boolean_varlist[i].value = 0;
  996.       return 0;
  997.     }
  998.     }
  999.  
  1000.   /* Not a boolean variable, so check for specials. */
  1001.  
  1002.   /* Editing mode change? */
  1003.   if (stricmp (name, "editing-mode") == 0)
  1004.     {
  1005.       if (strnicmp (value, "vi", 2) == 0)
  1006.     {
  1007. #if defined (VI_MODE)
  1008.       _rl_keymap = vi_insertion_keymap;
  1009.       rl_editing_mode = vi_mode;
  1010. #endif /* VI_MODE */
  1011.     }
  1012.       else if (strnicmp (value, "emacs", 5) == 0)
  1013.     {
  1014.       _rl_keymap = emacs_standard_keymap;
  1015.       rl_editing_mode = emacs_mode;
  1016.     }
  1017.     }
  1018.  
  1019.   /* Comment string change? */
  1020.   else if (stricmp (name, "comment-begin") == 0)
  1021.     {
  1022. #if defined (VI_MODE)
  1023.       if (*value)
  1024.     {
  1025.       if (rl_vi_comment_begin)
  1026.         free (rl_vi_comment_begin);
  1027.  
  1028.       rl_vi_comment_begin = savestring (value);
  1029.     }
  1030. #endif /* VI_MODE */
  1031.     }
  1032.   else if (stricmp (name, "completion-query-items") == 0)
  1033.     {
  1034.       int nval = 100;
  1035.       if (*value)
  1036.     {
  1037.       nval = atoi (value);
  1038.       if (nval < 0)
  1039.         nval = 0;
  1040.     }
  1041.       rl_completion_query_items = nval;
  1042.     }
  1043.   else if (stricmp (name, "keymap") == 0)
  1044.     {
  1045.       Keymap kmap;
  1046.       kmap = rl_get_keymap_by_name (value);
  1047.       if (kmap)
  1048.         rl_set_keymap (kmap);
  1049.     }
  1050.   else if (stricmp (name, "bell-style") == 0)
  1051.     {
  1052.       if (!*value)
  1053.         _rl_bell_preference = AUDIBLE_BELL;
  1054.       else
  1055.         {
  1056.           if (stricmp (value, "none") == 0 || stricmp (value, "off") == 0)
  1057.             _rl_bell_preference = NO_BELL;
  1058.           else if (stricmp (value, "audible") == 0 || stricmp (value, "on") == 0)
  1059.             _rl_bell_preference = AUDIBLE_BELL;
  1060.           else if (stricmp (value, "visible") == 0)
  1061.             _rl_bell_preference = VISIBLE_BELL;
  1062.         }
  1063.     }
  1064.   else if (stricmp (name, "prefer-visible-bell") == 0)
  1065.     {
  1066.       /* Backwards compatibility. */
  1067.       if (*value && (stricmp (value, "on") == 0 ||
  1068.              (*value == '1' && !value[1])))
  1069.         _rl_bell_preference = VISIBLE_BELL;
  1070.       else
  1071.         _rl_bell_preference = AUDIBLE_BELL;
  1072.     }
  1073.  
  1074.   return 0;
  1075. }
  1076.  
  1077. /* Return the character which matches NAME.
  1078.    For example, `Space' returns ' '. */
  1079.  
  1080. typedef struct {
  1081.   char *name;
  1082.   int value;
  1083. } assoc_list;
  1084.  
  1085. static assoc_list name_key_alist[] = {
  1086.   { "DEL", 0x7f },
  1087.   { "ESC", '\033' },
  1088.   { "Escape", '\033' },
  1089.   { "LFD", '\n' },
  1090.   { "Newline", '\n' },
  1091.   { "RET", '\r' },
  1092.   { "Return", '\r' },
  1093.   { "Rubout", 0x7f },
  1094.   { "SPC", ' ' },
  1095.   { "Space", ' ' },
  1096.   { "Tab", 0x09 },
  1097.   { (char *)0x0, 0 }
  1098. };
  1099.  
  1100. static int
  1101. glean_key_from_name (name)
  1102.      char *name;
  1103. {
  1104.   register int i;
  1105.  
  1106.   for (i = 0; name_key_alist[i].name; i++)
  1107.     if (stricmp (name, name_key_alist[i].name) == 0)
  1108.       return (name_key_alist[i].value);
  1109.  
  1110.   return (*(unsigned char *)name);    /* XXX was return (*name) */
  1111. }
  1112.  
  1113. /* Auxiliary functions to manage keymaps. */
  1114. static struct {
  1115.   char *name;
  1116.   Keymap map;
  1117. } keymap_names[] = {
  1118.   { "emacs", emacs_standard_keymap },
  1119.   { "emacs-standard", emacs_standard_keymap },
  1120.   { "emacs-meta", emacs_meta_keymap },
  1121.   { "emacs-ctlx", emacs_ctlx_keymap },
  1122. #if defined (VI_MODE)
  1123.   { "vi", vi_movement_keymap },
  1124.   { "vi-move", vi_movement_keymap },
  1125.   { "vi-command", vi_movement_keymap },
  1126.   { "vi-insert", vi_insertion_keymap },
  1127. #endif /* VI_MODE */
  1128.   { (char *)0x0, (Keymap)0x0 }
  1129. };
  1130.  
  1131. Keymap
  1132. rl_get_keymap_by_name (name)
  1133.      char *name;
  1134. {
  1135.   register int i;
  1136.  
  1137.   for (i = 0; keymap_names[i].name; i++)
  1138.     if (strcmp (name, keymap_names[i].name) == 0)
  1139.       return (keymap_names[i].map);
  1140.   return ((Keymap) NULL);
  1141. }
  1142.  
  1143. void
  1144. rl_set_keymap (map)
  1145.      Keymap map;
  1146. {
  1147.   if (map)
  1148.     _rl_keymap = map;
  1149. }
  1150.  
  1151. Keymap
  1152. rl_get_keymap ()
  1153. {
  1154.   return (_rl_keymap);
  1155. }
  1156.  
  1157. void
  1158. rl_set_keymap_from_edit_mode ()
  1159. {
  1160.   if (rl_editing_mode == emacs_mode)
  1161.     _rl_keymap = emacs_standard_keymap;
  1162. #if defined (VI_MODE)
  1163.   else if (rl_editing_mode == vi_mode)
  1164.     _rl_keymap = vi_insertion_keymap;
  1165. #endif /* VI_MODE */
  1166. }
  1167.  
  1168. /* **************************************************************** */
  1169. /*                                    */
  1170. /*          Key Binding and Function Information            */
  1171. /*                                    */
  1172. /* **************************************************************** */
  1173.  
  1174. /* Each of the following functions produces information about the
  1175.    state of keybindings and functions known to Readline.  The info
  1176.    is always printed to rl_outstream, and in such a way that it can
  1177.    be read back in (i.e., passed to rl_parse_and_bind (). */
  1178.  
  1179. /* Print the names of functions known to Readline. */
  1180. void
  1181. rl_list_funmap_names (count, ignore)
  1182.      int count, ignore;
  1183. {
  1184.   register int i;
  1185.   char **funmap_names;
  1186.  
  1187.   funmap_names = rl_funmap_names ();
  1188.  
  1189.   if (!funmap_names)
  1190.     return;
  1191.  
  1192.   for (i = 0; funmap_names[i]; i++)
  1193.     fprintf (rl_outstream, "%s\n", funmap_names[i]);
  1194.  
  1195.   free (funmap_names);
  1196. }
  1197.  
  1198. /* Return a NULL terminated array of strings which represent the key
  1199.    sequences that are used to invoke FUNCTION in MAP. */
  1200. char **
  1201. rl_invoking_keyseqs_in_map (function, map)
  1202.      Function *function;
  1203.      Keymap map;
  1204. {
  1205.   register int key;
  1206.   char **result;
  1207.   int result_index, result_size;
  1208.  
  1209.   result = (char **)NULL;
  1210.   result_index = result_size = 0;
  1211.  
  1212.   for (key = 0; key < 128; key++)
  1213.     {
  1214.       switch (map[key].type)
  1215.     {
  1216.     case ISMACR:
  1217.       /* Macros match, if, and only if, the pointers are identical.
  1218.          Thus, they are treated exactly like functions in here. */
  1219.     case ISFUNC:
  1220.       /* If the function in the keymap is the one we are looking for,
  1221.          then add the current KEY to the list of invoking keys. */
  1222.       if (map[key].function == function)
  1223.         {
  1224.           char *keyname = (char *)xmalloc (5);
  1225.  
  1226.           if (CTRL_CHAR (key))
  1227.         sprintf (keyname, "\\C-%c", to_lower (UNCTRL (key)));
  1228.           else if (key == RUBOUT)
  1229.         sprintf (keyname, "\\C-?");
  1230.           else if (key == '\\' || key == '"')
  1231.         {
  1232.           keyname[0] = '\\';
  1233.           keyname[1] = (char) key;
  1234.           keyname[2] = '\0';
  1235.         }
  1236.           else
  1237.         {
  1238.           keyname[0] = (char) key;
  1239.           keyname[1] = '\0';
  1240.         }
  1241.  
  1242.           if (result_index + 2 > result_size)
  1243.         result = (char **) xrealloc
  1244.           (result, (result_size += 10) * sizeof (char *));
  1245.  
  1246.           result[result_index++] = keyname;
  1247.           result[result_index] = (char *)NULL;
  1248.         }
  1249.       break;
  1250.  
  1251.     case ISKMAP:
  1252.       {
  1253.         char **seqs = (char **)NULL;
  1254.  
  1255.         /* Find the list of keyseqs in this map which have FUNCTION as
  1256.            their target.  Add the key sequences found to RESULT. */
  1257.         if (map[key].function)
  1258.           seqs =
  1259.             rl_invoking_keyseqs_in_map (function, FUNCTION_TO_KEYMAP (map, key));
  1260.  
  1261.         if (seqs)
  1262.           {
  1263.         register int i;
  1264.  
  1265.         for (i = 0; seqs[i]; i++)
  1266.           {
  1267.             char *keyname = (char *)xmalloc (6 + strlen (seqs[i]));
  1268.  
  1269.             if (key == ESC)
  1270.               sprintf (keyname, "\\e");
  1271.             else if (CTRL_CHAR (key))
  1272.               sprintf (keyname, "\\C-%c", to_lower (UNCTRL (key)));
  1273.             else if (key == RUBOUT)
  1274.               sprintf (keyname, "\\C-?");
  1275.             else if (key == '\\' || key == '"')
  1276.               {
  1277.             keyname[0] = '\\';
  1278.             keyname[1] = (char) key;
  1279.             keyname[2] = '\0';
  1280.               }
  1281.             else
  1282.               {
  1283.             keyname[0] = (char) key;
  1284.             keyname[1] = '\0';
  1285.               }
  1286.  
  1287.             strcat (keyname, seqs[i]);
  1288.             free (seqs[i]);
  1289.  
  1290.             if (result_index + 2 > result_size)
  1291.               result = (char **) xrealloc
  1292.             (result, (result_size += 10) * sizeof (char *));
  1293.  
  1294.             result[result_index++] = keyname;
  1295.             result[result_index] = (char *)NULL;
  1296.           }
  1297.  
  1298.         free (seqs);
  1299.           }
  1300.       }
  1301.       break;
  1302.     }
  1303.     }
  1304.   return (result);
  1305. }
  1306.  
  1307. /* Return a NULL terminated array of strings which represent the key
  1308.    sequences that can be used to invoke FUNCTION using the current keymap. */
  1309. char **
  1310. rl_invoking_keyseqs (function)
  1311.      Function *function;
  1312. {
  1313.   return (rl_invoking_keyseqs_in_map (function, _rl_keymap));
  1314. }
  1315.  
  1316. /* Print all of the current functions and their bindings to
  1317.    rl_outstream.  If an explicit argument is given, then print
  1318.    the output in such a way that it can be read back in. */
  1319. int
  1320. rl_dump_functions (count, key)
  1321.      int count, key;
  1322. {
  1323.   rl_function_dumper (rl_explicit_arg);
  1324.   rl_on_new_line ();
  1325.   return (0);
  1326. }
  1327.  
  1328. /* Print all of the functions and their bindings to rl_outstream.  If
  1329.    PRINT_READABLY is non-zero, then print the output in such a way
  1330.    that it can be read back in. */
  1331. void
  1332. rl_function_dumper (print_readably)
  1333.      int print_readably;
  1334. {
  1335.   register int i;
  1336.   char **names;
  1337.   char *name;
  1338.  
  1339.   names = rl_funmap_names ();
  1340.  
  1341.   fprintf (rl_outstream, "\n");
  1342.  
  1343.   for (i = 0; name = names[i]; i++)
  1344.     {
  1345.       Function *function;
  1346.       char **invokers;
  1347.  
  1348.       function = rl_named_function (name);
  1349.       invokers = rl_invoking_keyseqs_in_map (function, _rl_keymap);
  1350.  
  1351.       if (print_readably)
  1352.     {
  1353.       if (!invokers)
  1354.         fprintf (rl_outstream, "# %s (not bound)\n", name);
  1355.       else
  1356.         {
  1357.           register int j;
  1358.  
  1359.           for (j = 0; invokers[j]; j++)
  1360.         {
  1361.           fprintf (rl_outstream, "\"%s\": %s\n",
  1362.                invokers[j], name);
  1363.           free (invokers[j]);
  1364.         }
  1365.  
  1366.           free (invokers);
  1367.         }
  1368.     }
  1369.       else
  1370.     {
  1371.       if (!invokers)
  1372.         fprintf (rl_outstream, "%s is not bound to any keys\n",
  1373.              name);
  1374.       else
  1375.         {
  1376.           register int j;
  1377.  
  1378.           fprintf (rl_outstream, "%s can be found on ", name);
  1379.  
  1380.           for (j = 0; invokers[j] && j < 5; j++)
  1381.         {
  1382.           fprintf (rl_outstream, "\"%s\"%s", invokers[j],
  1383.                invokers[j + 1] ? ", " : ".\n");
  1384.         }
  1385.  
  1386.           if (j == 5 && invokers[j])
  1387.         fprintf (rl_outstream, "...\n");
  1388.  
  1389.           for (j = 0; invokers[j]; j++)
  1390.         free (invokers[j]);
  1391.  
  1392.           free (invokers);
  1393.         }
  1394.     }
  1395.     }
  1396. }
  1397.  
  1398. /* Bind key sequence KEYSEQ to DEFAULT_FUNC if KEYSEQ is unbound. */
  1399. void
  1400. _rl_bind_if_unbound (keyseq, default_func)
  1401.      char *keyseq;
  1402.      Function *default_func;
  1403. {
  1404.   Function *func;
  1405.  
  1406.   if (keyseq)
  1407.     {
  1408.       func = rl_function_of_keyseq (keyseq, _rl_keymap, (int *)NULL);
  1409.       if (!func || func == rl_do_lowercase_version)
  1410.     rl_set_key (keyseq, default_func, _rl_keymap);
  1411.     }
  1412. }
  1413.  
  1414. /* **************************************************************** */
  1415. /*                                    */
  1416. /*            String Utility Functions            */
  1417. /*                                    */
  1418. /* **************************************************************** */
  1419.  
  1420. static char *strindex ();
  1421.  
  1422. /* Return non-zero if any members of ARRAY are a substring in STRING. */
  1423. static int
  1424. substring_member_of_array (string, array)
  1425.      char *string, **array;
  1426. {
  1427.   while (*array)
  1428.     {
  1429.       if (strindex (string, *array))
  1430.     return (1);
  1431.       array++;
  1432.     }
  1433.   return (0);
  1434. }
  1435.  
  1436. #if !defined (HAVE_STRCASECMP)
  1437. /* Whoops, Unix doesn't have strnicmp. */
  1438.  
  1439. /* Compare at most COUNT characters from string1 to string2.  Case
  1440.    doesn't matter. */
  1441. static int
  1442. strnicmp (string1, string2, count)
  1443.      char *string1, *string2;
  1444.      int count;
  1445. {
  1446.   register char ch1, ch2;
  1447.  
  1448.   while (count)
  1449.     {
  1450.       ch1 = *string1++;
  1451.       ch2 = *string2++;
  1452.       if (to_upper(ch1) == to_upper(ch2))
  1453.     count--;
  1454.       else
  1455.         break;
  1456.     }
  1457.   return (count);
  1458. }
  1459.  
  1460. /* strcmp (), but caseless. */
  1461. static int
  1462. stricmp (string1, string2)
  1463.      char *string1, *string2;
  1464. {
  1465.   register char ch1, ch2;
  1466.  
  1467.   while (*string1 && *string2)
  1468.     {
  1469.       ch1 = *string1++;
  1470.       ch2 = *string2++;
  1471.       if (to_upper(ch1) != to_upper(ch2))
  1472.     return (1);
  1473.     }
  1474.   return (*string1 - *string2);
  1475. }
  1476. #endif /* !HAVE_STRCASECMP */
  1477.  
  1478. /* Determine if s2 occurs in s1.  If so, return a pointer to the
  1479.    match in s1.  The compare is case insensitive. */
  1480. static char *
  1481. strindex (s1, s2)
  1482.      register char *s1, *s2;
  1483. {
  1484.   register int i, l = strlen (s2);
  1485.   register int len = strlen (s1);
  1486.  
  1487.   for (i = 0; (len - i) >= l; i++)
  1488.     if (strnicmp (s1 + i, s2, l) == 0)
  1489.       return (s1 + i);
  1490.   return ((char *)NULL);
  1491. }
  1492.